﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Grundy.Message;
using Grundy.Message.Administration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Client.Tests
{
    // These tests require a license server at localhost:2225
    [TestClass]
    public class CheckoutTests
    {
        private static ClientConfiguration _clientConfiguration = ClientConfiguration.Instance;
        private RemoteLicenseResponse _response;
        const string CUSTOMER_KEY = "95FA6FF2A4C1E8AFBF724C4DACF4A41D1350A1DCA4BBA5F23078632F5A6569E74B283B4E21E0528B9B54FA364787563A6C72310B1582D7F83946CC5A3A07A4EE";

        [TestCleanup]
        public void TearDown()
        {
            if (_response != null && _response.IsSuccessful && _response.SessionIdentifier != Guid.Empty)
            {
                var client = GetLicenseClient();
                client.Checkin(_response);
            }
        }

        [TestMethod]
        public void Checkout_invalid_license()
        {
            var client = GetLicenseClient();
            var response = client.Checkout(new FeatureIdentificationKey {FeatureId = Guid.NewGuid()});
            Assert.IsFalse(response.IsSuccessful);
        }

        [TestMethod]
        public void Checkout_uncounted_floating_license()
        {
            var client = GetLicenseClient();
            _response = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Uncounted_Floating" }) as RemoteLicenseResponse;
            Assert.IsTrue(_response.IsSuccessful);
        }

        [TestMethod]
        public void Checkout_counted_floating_concurrent_1000_license()
        {
            var client = GetLicenseClient();
            _response = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Concurrent_1000" }) as RemoteLicenseResponse;
            Assert.IsTrue(_response.IsSuccessful);
        }

        [TestMethod]
        public void Checkout_counted_floating_concurrent_1_license()
        {
            var client = GetLicenseClient();
            _response = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Concurrent_1" }) as RemoteLicenseResponse;
            Assert.IsTrue(_response.IsSuccessful);
            var response2 = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Concurrent_1" });
            Assert.IsFalse(response2.IsSuccessful);
        }

        [TestMethod]
        public void Checkout_counted_floating_executions_1_license()
        {
            var client = GetLicenseClient();
            _response = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Executions_1" }) as RemoteLicenseResponse;
            Assert.IsTrue(_response.IsSuccessful);
            var response2 = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Executions_1" }) as RemoteLicenseResponse;
            Assert.IsFalse(response2.IsSuccessful);

            // Should be able to checkin execution counted license, however the execution count is still not decremented
            var response3 = client.Checkin(_response);
            Assert.IsTrue(response3.IsSuccessful);
            var response4 = client.Checkout(new FeatureIdentificationKey { FeatureName = "Dev_Counted_Floating_Executions_1" });
            Assert.IsFalse(response4.IsSuccessful);
        }

        [TestMethod]
        public void Get_licenses_installed_on_server()
        {
            var response = GetLicenseClient().ProcessAdministrationRequest(new GetInstalledLicensesRequest());
            Assert.IsTrue(response is Success);
        }

        [TestMethod]
        public void Checkout_node_locked_license()
        {
            var response = GetLicenseClient().Checkout(new FeatureIdentificationKey{FeatureName = "Dev_Node_Locked"});
            Assert.IsTrue(response.IsSuccessful);
        }

        public static LicenseClient GetLicenseClient()
        {
            return new LicenseClient(_clientConfiguration, CUSTOMER_KEY, "Dev_Customer");
        }
    }
}
